package demo1;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

class MyStack {
    Queue<Integer> qu1;
    Queue<Integer> qu2;

    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    //入栈
    public void push(int x) {
        if (empty()) {
            qu1.offer(x);
        } else if (!qu1.isEmpty()) {
            qu1.offer(x);
        } else {
            qu2.offer(x);
        }
    }
    //获得栈顶元素
    public int top () {
        if (empty()) {
            return -1;
        }
        int val = 0;
        if (!qu1.isEmpty()) {
            while (!qu1.isEmpty()) {
                val = qu1.poll();
                qu2.offer(val);
            }
        } else {
            while (!qu2.isEmpty()) {
                val = qu2.poll();
                qu1.offer(val);
            }
        }
        return val;
    }
    public int pop () {
        if (empty()) {
            return -1;
        }
        if (!qu1.isEmpty()) {
            int count = qu1.size();
            for (int i = 0; i < count - 1; i++) {
                qu2.offer(qu1.poll());
            }
            return qu1.poll();
        } else {
            int count = qu2.size();
            for (int i = 0; i < count- 1; i++) {
                qu1.offer(qu2.poll());
            }
            return qu2.poll();
        }
    }
    public boolean empty() {
        if (qu1.isEmpty() && qu2.isEmpty()) {
            return true;
        }
        return false;
    }
}

public class Test {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
    }
}
